clear
set more off
cap log close
set seed 1234

***************************************************************************************************
* 
* Program: analysis_PH_demo_voting.do
* Purpose: voting analysis file
*
* Sections:
*     0. Preliminary steps
*     1. Table 1 ("Impacts of Demolition and Relocation on Long-run Voting of Children") 
*     2. Figure 1 ("Impacts of Demolition and Relocation on Neighborhood Characteristics Over Time")
*     3. Figure 2 ("Impacts of Demolition and Relocation on General Election Voting")
*     4. Figure 3 ("Impacts of Demolition and Relocation on Voting, Earnings and Incarceration by Subgroup")
*
* Files Used:
*     1. analysis_file_voting_final.dta
***************************************************************************************************

global demo              ""
 // directory for storing demolition related files
global output            ""
 // directory to store figures/tables
global logs              ""
 // directory for log files
global dofiles           ""
 // directory where additional programs are stored

********************************************************************************
*** 0. Preliminary processing
********************************************************************************

cap net install seq, from(http://fmwww.bc.edu/RePEc/bocode/s/)

* load programs to make tables
do "${dofiles}6_supplemental_programs_final.do"

********************************************************************************
*** 1. Table 1: Impacts of Demolition and Relocation on Long-run Voting of Children
********************************************************************************

* set globals for table creation
global cov "male black ms_male ms_black i.proj_code"
global voting_var "voted_general voted_primary_ever general_2016 general_2012 general_2008 general_2004 pres_voted_share registered_anything registered_nonpartisan registered_republican registered_democrat"

* main results
gzuse "${demo}analysis_file_voting_final.dta.gz", clear
global age_range "xxage>=5&xxage<=18"
voting_table proj_code~=1 voting_var table1_voting ${age_range}

********************************************************************************
*** Figure 1: Impacts of Demolition and Relocation on Neighborhood Characteristics Over Time
********************************************************************************

gzuse "${demo}analysis_file_voting_final.dta.gz", clear

global age_range "xxage>=5 & xxage<=18"

*** Set Sample
drop if proj_grp=="HR 1995" 
*drop if proj_grp=="WE 1995" 
drop if (xxbdg=="WE002" | xxbdg=="WE006") 
 // low-rise public housing
 
* restrict to voting sample
keep if voting_sample==1 & ${age_range}

* tag one observation per year per household
egen tag_hh_year = tag(household_id year)

* flatten to one observation per household per year
keep if tag_hh_year==1

* tag one observation per household
egen tag_hh = tag(household_id)

*** figure for neighborhood characteristics over time

global yscale1 = "-.205(.1).205"
global yscale2 = "#4"
global yscale3 = "#4"
global yscale4 = "#4"
global yscale5 = "#4"
global yscale6 = "-.205(.1).205"

global title1 = "(a) HH Has Address"
global title2 = "(b) Black (Pct. Points)"
global title3 = "(c) Below Poverty (Pct. Points)"
global title4 = "(d) Violent Crime per 10k"
global title5 = "(e) Property Crime per 10k"
global title6 = "(f) Vote Fraction"

global setting1 = "%9.1fc"
global setting2 = "%9.0fc"
global setting3 = "%9.0fc"
global setting4 = "%9.0fc"
global setting5 = "%9.0fc"
global setting6 = "%9.1fc"

* set local "run", which will control figure settings
local run=1
	
* loop over outcomes
foreach var of varlist has_addr_panel pblack pbpov nbh_violent {
	
	preserve 
	
	xi: reg `var' pre* post* treat_pre* treat_post* i.proj_grp if (t<=10 & t>=-5), cluster(xxbdg) nocons

	* Save estimates
	gen te=.
	gen ub=.
	gen lb=. 

	forvalues i=1(1)10 {
	 replace te = _b[treat_post`i'] if t==`i'
	 replace ub = _b[treat_post`i']+1.96*_se[treat_post`i'] if t==`i'
	 replace lb = _b[treat_post`i']-1.96*_se[treat_post`i']  if t==`i'
	 }
	 
	forvalues i=1(1)5 {
	 replace te = _b[treat_pre`i'] if t==-`i'
	 replace ub = _b[treat_pre`i']+1.96*_se[treat_post`i'] if t==-`i'
	 replace lb = _b[treat_pre`i']-1.96*_se[treat_post`i']  if t==-`i'
	 }
	 
	replace te = 0 if t==0
	replace ub = 0 if t==0
	replace lb = 0 if t==0
	
	keep t te ub lb
	duplicates drop

	drop if t<-5
	drop if t>10
	
	sort t
		
	twoway (connected te t, lcolor(black) mcolor(black)) ///
		   (connected lb t, lpattern(dash) lcolor(gray) m(circle_hollow) mcolor(gray)) ///
		   (connected ub t, lpattern(dash) lcolor(gray) m(circle_hollow) mcolor(gray)), ///
		   xtitle("") legend(off) ///
		   title("${title`run'}") ///
		   yline(0, lcolor(black)) ytitle("", axis(1)) ///
		   ylabel(${yscale`run'}, format(${setting`run'}) axis(1) glcolor(gray*.35) glpattern(dash)) ///
		   xlabel(-4(2)10) ///
		   xscale(titlegap(2)) ///
		   xline(0) legend(off) yscale(titlegap(2) axis(1)) ///
		   graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))  
 
	graph save "${output}figure_nbhd_`var'_over_time.gph", replace

	restore
	
	local run=`run'+1

}
	
* combine individual plots into a single figure with 4 panels
graph combine ${output}figure_nbhd_has_addr_panel_over_time.gph ///
			  ${output}figure_nbhd_pblack_over_time.gph ///
			  ${output}figure_nbhd_pbpov_over_time.gph ///
			  ${output}figure_nbhd_nbh_violent_over_time.gph, ///
			  graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
 	
graph save "${output}figure1_nbhd_te_over_time.gph", replace
graph export "${output}figure1_nbhd_te_over_time.pdf", replace

********************************************************************************
*** 3. Figure 2: Impacts of Demolition and Relocation on General Election Voting
********************************************************************************

*** Displaced versus non-displaced voting in general elections (bar chart)

gzuse "${demo}analysis_file_voting_final.dta.gz", clear

global age_range "xxage>=5 & xxage<=18"
global cov "male black ms_male ms_black i.proj_code"

prep_data

preserve

* loop over general election outcomes
forvalues i=2008(2)2018 {
	xi: reg general_`i' treat ${cov} if ${age_range} & proj_code~=1, cluster(xxbdg)
	sum general_`i' if e(sample)==1
	* store results
	local control_`i' = r(mean)
	local b_`i' = _b[treat]
	local se_`i' = _se[treat]
}

* store results as a separate file
clear 
set obs 12

* sequence variables
seq group, f(1) t(2)
 // treatment versus control
seq outcome, b(2) f(1) t(6)
 // six outcomes

* variables for outcomes and standard errors
gen y = . 
gen se = .

local l = 1

* bars
forvalues i=2008(2)2018 {
  replace y = `control_`i'' if group==1 & outcome==`l' // control group bar
  replace y = `control_`i'' + `b_`i'' if group==2 & outcome==`l' // treatment group bar
  replace se = `se_`i'' if group==2 & outcome==`l' // standard error
  local l = `l'+1
  }

gen y_upper = y+1.96*se
gen y_lower = y-1.96*se

list

gen outcomegroup=group if outcome==1
replace outcomegroup=group+3 if outcome==2
replace outcomegroup=group+6 if outcome==3
replace outcomegroup=group+9 if outcome==4
replace outcomegroup=group+12 if outcome==5
replace outcomegroup=group+15 if outcome==6

* figure
graph twoway (bar y outcomegroup if group==1) ///
 (bar y outcomegroup if group==2) ///
 (bar y outcomegroup if group==3) ///
 (rcap y_upper y_lower outcomegroup, lcolor(black) lwidth(vthin)), ///
 legend(order (1 "Non-displaced" 2 "Displaced") region(lcolor(white))) ///
 xlabel(1.5 "2008" 4.5 "2010" 7.5 "2012" 10.5 "2014" 13.5 "2016" 16.5 "2018", noticks) ///
 ytitle("Mean Voting Rate") ///
 yscale(range(0 .25)) ylabel(0 0.05 0.1 0.15 0.2 0.25) xtitle("") ///
 title("") ///
 graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///

graph save "${output}figure2_general_voting.gph", replace
graph export "${output}figure2_general_voting.pdf", replace

restore

********************************************************************************
*** 4. Figure 3: Impacts of Demolition and Relocation on Voting, Incarceration, Employment by Subgroup
********************************************************************************

gzuse "${demo}analysis_file_final.dta.gz", clear

global age_range "xxage>=5 & xxage<=18"

do "${dofiles}6_supplemental_programs_final.do"

prep_data

*** Set Sample
drop if proj_grp=="HR 1995" 
*drop if proj_grp=="WE 1995" 
drop if (xxbdg=="WE002" | xxbdg=="WE006") 
 // low-rise public housing
 
global cov "male black ms_male ms_black i.proj_code"
global age "xxageless12 xxage1218 xxageless12_treat xxage1218_treat"
global nbhd "high_demo low_demo high_demo_treat low_demo_treat"
global sex "male female male_treat female_treat"
global work "hh_xxnowork hh_xxnowork_treat hh_xxwork hh_xxwork_treat"
global crime "hh_xxnocrime hh_xxcrime_treat hh_xxcrime hh_xxnocrime_treat"

* begin loop for cross-sectional outcomes
foreach var of varlist voted_general sentenced_imprison {
    
	* below code generates a table + figure of results 	
	file close _all
	file open table using "${output}table_subgroup_effects_`var'.tex", write replace

	* row for pooled result

	file write table "All " 
	file write table "&"
	file write table "1.00"

	sum `var' if treat==0 & $age_range & tag==1
	file write table "&" (string(r(mean),"%15.3fc"))

	xi: reg `var' treat $cov if $age_range & tag==1, cluster(xxbdg)
	test treat
	local p_val = r(p)

	local sig ""
	if `p_val'<.1 local sig "*"
	if `p_val'<.05 local sig "**"
	if `p_val'<.01 local sig "***"
	 
	file write table "&" (string(_b[treat],"%15.3fc")) "`sig'"
 
	* # persons
	xi: reg `var' treat $cov if $age_range & tag==1, cluster(xxbdg)
	egen temp = tag(chdhsid_201212) if e(sample)==1
	count if temp==1
	file write table "&" (string(r(N),"%15.0gc"))
	drop temp

	file write table " \\"

	file write table _n // end means/estimates row

	* Now compute standard errors

	file write table "&"
	file write table "&"

	xi: reg `var' treat $cov if $age_range & tag==1, robust cluster(xxbdg)
	file write table "& (" (string(_se[treat],"%15.3fc")) ")" 

	file write table " \\"
	 
	file write table _n // end standard error row
	
	* loop over subgroups	
	foreach group in male female xxageless12 xxage1218 high_demo low_demo hh_xxnowork hh_xxwork hh_xxnocrime hh_xxcrime {

		display "`group'"
		
		* main group headers

		if "`group'"=="xxageless12" {
			file write table "\textbf{Age at Baseline}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="male" {
			file write table "\textbf{Sex}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="high_demo" {
			file write table "\textbf{Poverty Rate}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="hh_xxnowork" {
			file write table "\textbf{HH Adult Employment}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="hh_xxnocrime" {
			file write table "\textbf{HH Past Arrests}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		* subgroup rows
			
		if "`group'"=="xxageless12" {
			file write table "\quad 5-11"
			}
			
		if "`group'"=="xxage1218" {
			file write table "\quad 12-18"
			}
			
		if "`group'"=="male" {
			file write table "\quad Male"
			}
			
		if "`group'"=="female" {
			file write table "\quad Female"
			}
			
		if "`group'"=="high_demo" {
			file write table "\quad Higher"
			}
			
		if "`group'"=="low_demo" {
			file write table "\quad Lower"
			}
			
		if "`group'"=="hh_xxnowork" {
			file write table "\quad No Working Adults"
			}
			
		if "`group'"=="hh_xxwork" {
			file write table "\quad $> 0$ Working Adults"
			}
			
		if "`group'"=="hh_xxnocrime" {
			file write table "\quad No Adults with Arrest(s)"
			}
			
		if "`group'"=="hh_xxcrime" {
			file write table "\quad $> 0$ Adults with Arrest(s)"
			}
		
		* Number of children
		count if tag==1 & $age_range & `group'==1
		*file write table "&" (char(34)) (string(r(N),"%15.0fc")) (char(34))
			
		* Fraction of Sample
		sum `group' if tag==1 & $age_range
		file write table "&" (string(r(mean),"%15.2fc")) 
			
		* Control Group Mean
		sum `var' if treat==0 & $age_range & `group'==1
		file write table "&" (string(r(mean),"%15.3fc"))
		
		* Regressions
		if "`group'"=="xxageless12" | "`group'"=="xxage1218" {
			xi: reg `var' $cov $age if tag==1 & $age_range, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test xxageless12_treat==xxage1218_treat
			
			global age_511=_b[xxageless12_treat]
			global age_511_se=_se[xxageless12_treat]

			global age_1218=_b[xxage1218_treat]
			global age_1218_se=_se[xxage1218_treat]
					
			}
			
		if "`group'"=="high_demo" | "`group'"=="low_demo" {
			xi: reg `var' $cov $nbhd if tag==1 & $age_range, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test high_demo_treat==low_demo_treat		

			global pov_low=_b[low_demo_treat]
			global pov_low_se=_se[low_demo_treat]

			global pov_high=_b[high_demo_treat]
			global pov_high_se=_se[high_demo_treat]

			}
			
		if "`group'"=="male" | "`group'"=="female" {
			xi: reg `var' $cov $sex if tag==1 & $age_range, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test male_treat==female_treat		
			
			global sex_m=_b[male_treat]
			global sex_m_se=_se[male_treat]

			global sex_f=_b[female_treat]
			global sex_f_se=_se[female_treat]
			
			}
			
		if "`group'"=="hh_xxwork" | "`group'"=="hh_xxnowork" {
			xi: reg `var' $cov $work if tag==1 & $age_range, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test hh_xxwork==hh_xxnowork	
			
			global hh_work0=_b[hh_xxnowork_treat]
			global hh_work0_se=_se[hh_xxnowork_treat]

			global hh_work1=_b[hh_xxwork_treat]
			global hh_work1_se=_se[hh_xxwork_treat]
			
			}
		
		
		if "`group'"=="hh_xxcrime" | "`group'"=="hh_xxnocrime" {
			xi: reg `var' $cov $crime if tag==1 & $age_range, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test hh_xxcrime==hh_xxnocrime		
			
			global hh_crime0=_b[hh_xxnocrime_treat]
			global hh_crime0_se=_se[hh_xxnocrime_treat]

			global hh_crime1=_b[hh_xxcrime_treat]
			global hh_crime1_se=_se[hh_xxcrime_treat]
			
			}
			
		test `group'_treat
		local p_val = r(p)
				
		local sig ""
		if `p_val'<.1 local sig "*"
		if `p_val'<.05 local sig "**"
		
		file write table "&" (string(_b[`group'_treat],"%15.3fc")) "`sig'"

		* # of observations
		count if e(sample)==1
		*file write table "&" (char(34)) (string(r(N),"%15.0gc")) (char(34))
		
		* # of individuals
		egen temp = tag(chdhsid_201212) if e(sample)==1 & `group'==1
		count if temp==1
		file write table "&" (string(r(N),"%15.0gc"))
		
		drop temp
		
		file write table " \\"
		
		file write table _n // end row
		
		*file write table "&" 
		
		file write table "&" 
		
		file write table "&" 
		
		file write table "&" "(" (string(_se[`group'_treat],"%15.3fc")) ")"

		file write table " \\"
		
		file write table _n // end row

	} // end of loop for subgroups
	
	file close table // close table
	display "END"

	* graph settings
	global margin "r=37"
	global boxgap=40
	global outergap=55
	global labgap=2.5
	global fxsize=31.5
	global angle="0"
	
	preserve
	clear

	set obs 5 // 5 numbers to store (end of 95 pct, 1 SE below, estimate, 1 SE above, top of 95 pct)

	gen age=.
	gen age_511=.
	gen age_1218=.

	gen sex=.
	gen sex_m=.
	gen sex_f=.

	gen pov=.
	gen pov_low=.
	gen pov_high=.

	gen hh_work=.
	gen hh_work0=.
	gen hh_work1=.

	gen hh_crime=.
	gen hh_crime0=.
	gen hh_crime1=.

	* create globals based on saved estimates
	foreach group of varlist sex_m sex_f age_511 age_1218 pov_low pov_high hh_work0 hh_work1 hh_crime0 hh_crime1 {
		global `group'_lw=${`group'}-1.96*${`group'_se}
		global `group'_lb=${`group'}-${`group'_se}
		global `group'_ub=${`group'}+${`group'_se}
		global `group'_uw=${`group'}+1.96*${`group'_se}
	}

	* store estimates
	foreach group of varlist sex_m sex_f age_511 age_1218 pov_low pov_high hh_work0 hh_work1 hh_crime0 hh_crime1 {
		replace `group'=${`group'_lw} if _n==1
		replace `group'=${`group'_lb} if _n==2
		replace `group'=${`group'} if _n==3
		replace `group'=${`group'_ub} if _n==4
		replace `group'=${`group'_uw} if _n==5
		}
		
	list
	
	if "`var'"=="sentenced_imprison" {
	 *local label="Ever Incarcerated (Up to 2012)"
	 local label="Incarcerated (Up to '12)"
	 }
	 
	if "`var'"=="voted_general" {
	 local label="Ever Voted"
	 }

	* create individual figure
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(%9.2fc) axis(1) glcolor(gray*.35) glpattern(dash)) ///
		title("`label'") ///
		showyvars yvaroptions(relabel( ///
		1 `"`"{bf:Sex:}"'"' 2 `"`"Male"'"' 3 `"`"Female"'"' ///
		4 `"`"{bf:Age:}"'"' 5 `"`"5-11"'"' 6 `"`"12-18"'"' /// 
		7 `"`"{bf:Poverty Rate:}"'"' 8 `"`"Higher"'"' 9 `"`"Lower"'"' ///
		10 `"`"{bf:HH Work:}"'"' 11 `"`"No adult working"'"' 12 `"`"Adult working"'"' ///
		13 `"`"{bf:HH Arrest}:"'"' 14 `"`"No arrested adults"'"' 15 `"`"Adult arrested"'"') ///
		label(labgap($labgap) labsize(normal))) /// 
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))

	graph save "${output}figure_subgroup_effects_`var'.gph", replace
	graph export "${output}figure_subgroup_effects_`var'.pdf", replace
	
	* version of figure without y-axis labels
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(`format') axis(1) glcolor(gray*.35) glpattern(dash) angle($angle) labsize(small)) ///
		title("`label'") ///
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
		** notes: option "gap" makes the boxes closer together
		
	graph save "${output}figure_subgroup_effects_`var'_nolabel_nomargin.gph", replace
	
	* version of figure with angle label option and small size option
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(%9.2fc) axis(1) glcolor(gray*.35) glpattern(dash) angle($angle) labsize(small)) ///
		title("`label'") ///
		showyvars yvaroptions(relabel( ///
		1 `"`"{bf:Sex:}"'"' 2 `"`"Male"'"' 3 `"`"Female"'"' ///
		4 `"`"{bf:Age:}"'"' 5 `"`"5-11"'"' 6 `"`"12-18"'"' /// 
		7 `"`"{bf:Poverty Rate:}"'"' 8 `"`"Higher"'"' 9 `"`"Lower"'"' ///
		10 `"`"{bf:HH Work:}"'"' 11 `"`"No adult working"'"' 12 `"`"Adult working"'"' ///
		13 `"`"{bf:HH Arrest}:"'"' 14 `"`"No arrested adults"'"' 15 `"`"Adult arrested"'"') ///
		label(labgap($labgap) labsize(small))) /// 
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))

	graph save "${output}figure_subgroup_effects_`var'_anglelab.gph", replace
	
	restore
	
} // end of loop for variables

* loop for panel outcomes
foreach var of varlist employed_yr {

	file close _all
	file open table using "${output}table_subgroup_effects_`var'.tex", write replace	
	
	* row for pooled result

	file write table "All " 
	file write table "&"
	file write table "1.00"

	sum `var' if treat==0 & $age_range & age_yr>18
	file write table "&" (string(r(mean),"%15.3fc"))

	xi: reg `var' treat $cov if $age_range & age_yr>18, cluster(xxbdg)
	test treat
	local p_val = r(p)

	local sig ""
	if `p_val'<.1 local sig "*"
	if `p_val'<.05 local sig "**"
	if `p_val'<.01 local sig "***"
	 
	file write table "&" (string(_b[treat],"%15.3fc")) "`sig'"
	 
	* # persons
	qui cap xi: reg `var' treat $cov if $age_range & age_yr>18, cluster(xxbdg)
	egen temp = tag(chdhsid_201212) if e(sample)==1
	count if temp==1
	file write table "&" (string(r(N),"%15.0gc"))
	drop temp

	file write table " \\"

	file write table _n // end means/estimates row

	* Now compute standard errors

	file write table "&"
	file write table "&"

	xi: reg `var' treat $cov if $age_range & age_yr>18, robust cluster(xxbdg)
	file write table "& (" (string(_se[treat],"%15.3fc")) ")" 

	file write table " \\"
	 
	file write table _n // end standard error row

	foreach group in male female xxageless12 xxage1218 high_demo low_demo hh_xxnowork hh_xxwork hh_xxnocrime hh_xxcrime {

		display "`group'"
		
		* main group headers
		
		if "`group'"=="xxageless12" {
			file write table "\textbf{Age at Baseline}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="male" {
			file write table "\textbf{Sex}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="high_demo" {
			file write table "\textbf{Poverty Rate}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="hh_xxnowork" {
			file write table "\textbf{HH Adult Employment}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		if "`group'"=="hh_xxnocrime" {
			file write table "\textbf{HH Past Arrests}"
			file write table "\\ \\[-1.5ex]"
			file write table _n // end row
			}
			
		* subgroup rows
			
		if "`group'"=="xxageless12" {
			file write table "\quad 5-11"
			}
			
		if "`group'"=="xxage1218" {
			file write table "\quad 12-18"
			}
			
		if "`group'"=="male" {
			file write table "\quad Male"
			}
			
		if "`group'"=="female" {
			file write table "\quad Female"
			}
			
		if "`group'"=="high_demo" {
			file write table "\quad Higher"
			}
			
		if "`group'"=="low_demo" {
			file write table "\quad Lower"
			}
			
		if "`group'"=="hh_xxnowork" {
			file write table "\quad No Working Adults"
			}
			
		if "`group'"=="hh_xxwork" {
			file write table "\quad $> 0$ Working Adults"
			}
			
		if "`group'"=="hh_xxnocrime" {
			file write table "\quad No Adults with Arrest(s)"
			}
			
		if "`group'"=="hh_xxcrime" {
			file write table "\quad $> 0$ Adults with Arrest(s)"
			}
		
		* Number of children
		count if tag==1 & $age_range & `group'==1
		*file write table "&" (char(34)) (string(r(N),"%15.0fc")) (char(34))
			
		* Fraction of Sample
		sum `group' if tag==1 & $age_range
		file write table "&" (string(r(mean),"%15.2fc"))
			
		* Control Group Mean
		sum `var' if treat==0 & $age_range & `group'==1 & age_yr>18
		file write table "&" (string(r(mean),"%15.3fc"))
		
		* Regressions
		if "`group'"=="xxageless12" | "`group'"=="xxage1218" {
			xi: reg `var' $cov $age if $age_range & age_yr>18, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test xxageless12_treat==xxage1218_treat
			
			global age_511=_b[xxageless12_treat]
			global age_511_se=_se[xxageless12_treat]

			global age_1218=_b[xxage1218_treat]
			global age_1218_se=_se[xxage1218_treat]
					
			}
			
		if "`group'"=="high_demo" | "`group'"=="low_demo" {
			xi: reg `var' $cov $nbhd if $age_range & age_yr>18, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test high_demo_treat==low_demo_treat		

			global pov_low=_b[low_demo_treat]
			global pov_low_se=_se[low_demo_treat]

			global pov_high=_b[high_demo_treat]
			global pov_high_se=_se[high_demo_treat]

			}
			
		if "`group'"=="male" | "`group'"=="female" {
			xi: reg `var' $cov $sex if $age_range & age_yr>18, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test male_treat==female_treat		
			
			global sex_m=_b[male_treat]
			global sex_m_se=_se[male_treat]

			global sex_f=_b[female_treat]
			global sex_f_se=_se[female_treat]
			
			}
			
		if "`group'"=="hh_xxwork" | "`group'"=="hh_xxnowork" {
			xi: reg `var' $cov $work if $age_range & age_yr>18, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test hh_xxwork==hh_xxnowork	
			
			global hh_work0=_b[hh_xxnowork_treat]
			global hh_work0_se=_se[hh_xxnowork_treat]

			global hh_work1=_b[hh_xxwork_treat]
			global hh_work1_se=_se[hh_xxwork_treat]
			}
		
	
		if "`group'"=="hh_xxcrime" | "`group'"=="hh_xxnocrime" {
			xi: reg `var' $cov $crime if $age_range & age_yr>18, cluster(xxbdg) nocons
			
			display "Test equality of TEs"
			test hh_xxcrime==hh_xxnocrime		
			
			global hh_crime0=_b[hh_xxnocrime_treat]
			global hh_crime0_se=_se[hh_xxnocrime_treat]

			global hh_crime1=_b[hh_xxcrime_treat]
			global hh_crime1_se=_se[hh_xxcrime_treat]
			}
			
		test `group'_treat
		local p_val = r(p)
				
		local sig ""
		if `p_val'<.1 local sig "*"
		if `p_val'<.05 local sig "**"
		
		file write table "&" (string(_b[`group'_treat],"%15.3fc")) "`sig'" 

		* # of observations
		count if e(sample)==1
		*file write table "&" (char(34)) (string(r(N),"%15.0gc")) (char(34))
		
		* # of individuals
		egen temp = tag(chdhsid_201212) if e(sample)==1 & `group'==1
		count if temp==1
		file write table "&" (string(r(N),"%15.0gc")) 
		
		drop temp
		
		file write table "\\"
		
		file write table _n // end row
		
		file write table "&" 
		
		*file write table "&" 
		
		file write table "&" 
		
		file write table "&" "(" (string(_se[`group'_treat],"%15.3fc")) ")" 

		file write table "\\"
		
		file write table _n // end row

	}

	file close table // close table
	display "END"
		
	preserve
	clear

	set obs 5 // 3 estimates and each one has 5 numbers associated with it

	gen age=.
	gen age_511=.
	gen age_1218=.

	gen sex=.
	gen sex_m=.
	gen sex_f=.

	gen pov=.
	gen pov_low=.
	gen pov_high=.
	
	gen hh_work=.
	gen hh_work0=.
	gen hh_work1=.

	gen hh_crime=.
	gen hh_crime0=.
	gen hh_crime1=.

	foreach group of varlist sex_m sex_f age_511 age_1218 pov_low pov_high hh_work0 hh_work1 hh_crime0 hh_crime1 {
		global `group'_lw=${`group'}-1.96*${`group'_se}
		global `group'_lb=${`group'}-${`group'_se}
		global `group'_ub=${`group'}+${`group'_se}
		global `group'_uw=${`group'}+1.96*${`group'_se}
	}

	foreach group of varlist sex_m sex_f age_511 age_1218 pov_low pov_high hh_work0 hh_work1 hh_crime0 hh_crime1 {
		replace `group'=${`group'_lw} if _n==1
		replace `group'=${`group'_lb} if _n==2
		replace `group'=${`group'} if _n==3
		replace `group'=${`group'_ub} if _n==4
		replace `group'=${`group'_uw} if _n==5
		}
		
	list
	
	if "`var'"=="wages_yr" {
	 local label="Earnings"
	 local format="%9.0fc"
	 }
	 
	if "`var'"=="employed_yr" {
	 local label="Employed"
	 local format="%9.2fc"
	 }
	
	if "`var'"=="violent_yr" {
	 local label="Violent Arrests"
	 local format="%9.2fc"
	 }

	* stand-alone figure
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(`format') axis(1) glcolor(gray*.35) glpattern(dash)) ///
		title("`label'") ///
		showyvars yvaroptions(relabel( ///
		1 `"`"{bf:Sex:}"'"' 2 `"`"Male"'"' 3 `"`"Female"'"' ///
		4 `"`"{bf:Age:}"'"' 5 `"`"5-11"'"' 6 `"`"12-18"'"' /// 
		7 `"`"{bf:Poverty Rate:}"'"' 8 `"`"Higher"'"' 9 `"`"Lower"'"' ///
		10 `"`"{bf:HH Work:}"'"' 11 `"`"No adult working"'"' 12 `"`"Adult working"'"' ///
		13 `"`"{bf:HH Arrest}:"'"' 14 `"`"No arrested adults"'"' 15 `"`"Adult arrested"'"') ///
		label(labgap($labgap) labsize(normal))) /// 
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))

	graph save "${output}figure_subgroup_effects_`var'.gph", replace
	graph export "${output}figure_subgroup_effects_`var'.pdf", replace
	
	* figure without y-axis labels
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(`format') axis(1) glcolor(gray*.35) glpattern(dash) angle($angle)) ///
		title("`label'") ///
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(margin($margin) color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
		** notes: option "gap" makes the boxes closer together
		
	graph save "${output}figure_subgroup_effects_`var'_nolabel.gph", replace
	graph export "${output}figure_subgroup_effects_`var'_nolabel.pdf", replace
	
	* figure without y-axis labels
	graph hbox sex sex_m sex_f age age_511 age_1218 pov pov_high pov_low hh_work hh_work0 hh_work1 hh_crime hh_crime0 hh_crime1, ///
		box(2, fcolor(navy) lcolor(navy)) box(3, fcolor(maroon) lcolor(maroon)) ///
		box(5, fcolor(navy) lcolor(navy)) box(6, fcolor(maroon) lcolor(maroon)) ///
		box(8, fcolor(navy) lcolor(navy)) box(9, fcolor(maroon) lcolor(maroon)) ///
		box(11, fcolor(navy) lcolor(navy)) box(12, fcolor(maroon) lcolor(maroon)) ///
		box(14, fcolor(navy) lcolor(navy)) box(15, fcolor(maroon) lcolor(maroon)) ///
		yscale(titlegap(2) axis(1)) ///
		yline(0, lcolor(gray)) ///
		ylabel(, format(`format') axis(1) glcolor(gray*.35) glpattern(dash) angle($angle) labsize(small)) ///
		title("`label'") ///
		outergap($outgap) boxgap($boxgap) legend(off) graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white))
		** notes: option "gap" makes the boxes closer together
		
	graph save "${output}figure_subgroup_effects_`var'_nolabel_nomargin.gph", replace
	
	restore

}

*** Compile individual figures to create a single figure with 3 panels
********************************************************************************

graph combine "${output}figure_subgroup_effects_voted_general_anglelab.gph" "${output}figure_subgroup_effects_sentenced_imprison_nolabel_nomargin.gph" "${output}figure_subgroup_effects_employed_yr_nolabel_nomargin.gph", ///
xsize(7) ///
graphregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) plotregion(color(white) fcolor(white) icolor(white) ifcolor(white) lcolor(white) ilcolor(white)) ///
cols(3)

graph save "${output}figure3_subgroup_effects_voting_imprison_employed_combined.gph", replace
graph export "${output}figure3_subgroup_effects_voting_imprison_employed_combined.pdf", replace








